package com.john.market.activities;

import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.database.SQLException;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.Toast;

import com.john.market.R;
import com.john.market.classes.ProductManager;
import com.john.market.classes.DBStorage;
import com.john.market.interfaces.IUpdatable;
import com.john.market.interfaces.IShop;
import com.john.market.interfaces.IStock;

/**
 *	Main activity
 *
 */
public class MarketActivity extends TabActivity {
	// Tabs tags
	private final String TAG_SHOP = "shop";
	private final String TAG_STOCK = "stock";
	
	// Tabs labels
	private final String LABEL_SHOP = null;
	private final String LABEL_STOCK = null;
	
	 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        // Create data storage and check it
        
        DBStorage storage = null;
        
        try {
        	storage = new DBStorage(this);
        }
        catch (SQLException e) {
        	Toast.makeText(this, "Cannot open database!", Toast.LENGTH_LONG).show();
        	finish();
        }
        
        // Create ProductManager
        ProductManager manager = new ProductManager(storage);
        
        // Prepare resources, tab host and intent
        Resources res = getResources();
        TabHost tabHost = getTabHost();
        TabHost.TabSpec spec;
        Intent intent;
        
        // Adding tab for the shop
        intent = new Intent().setClass(this, ShopActivity.class);
        spec = tabHost.newTabSpec(TAG_SHOP).setIndicator(LABEL_SHOP, res.getDrawable(R.drawable.shop)).setContent(intent);
        tabHost.addTab(spec);
        
        // Adding tab for the stock
        intent = new Intent().setClass(this, StockActivity.class);
        spec = tabHost.newTabSpec(TAG_STOCK).setIndicator(LABEL_STOCK, res.getDrawable(R.drawable.store)).setContent(intent);
        tabHost.addTab(spec);
        
        // Setting the stock activity
        tabHost.setCurrentTabByTag(TAG_STOCK);
        IStock stock = (IStock)getLocalActivityManager().getActivity(TAG_STOCK);
        if (stock != null) {
        	manager.addListener((IUpdatable)stock);
			stock.setSeller(manager);
        }
        
        // Setting the shop activity
        tabHost.setCurrentTabByTag(TAG_SHOP);
        IShop shop = (IShop)getLocalActivityManager().getActivity(TAG_SHOP);
        if (shop != null) {
        	manager.addListener((IUpdatable)shop);
			shop.setBuyer(manager);
        }
        
        // Setting listener for refreshing selected tab
        tabHost.setOnTabChangedListener(new OnTabChangeListener() {
			@Override
			public void onTabChanged(String tag) {
				final IUpdatable activity = (IUpdatable)getLocalActivityManager().getActivity(tag);
				if (activity != null) {
					activity.onNeedRefresh();
				}
				
			}
			
		});
        
        // Refresh list of products at start
        storage.refresh();
        
    }
   
}